package com.example.myapplication.tack_24;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.example.myapplication.R;
import com.example.myapplication.tack_24.adapter.ArrangeAdapter;
import com.example.myapplication.tack_24.adapter.FenLeiAdapter;
import com.example.myapplication.tack_24.data.BannerData;
import com.example.myapplication.tack_24.data.FenLeiData;
import com.example.myapplication.utils.BannerTool;
import com.example.myapplication.utils.OkHttpTool;

import java.util.ArrayList;

public class LogisticsActivity extends AppCompatActivity {
    private ImageView exit;
    private ViewPager banner1;
    private RecyclerView fenlei;
    private RecyclerView arrange;
    private SearchView search;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_24_activity_logistics);
        init();
        getBanner();
        getWhole();
        exit_();
        getSearch();
    }

    private void getSearch() {
        search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                submit(query);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                change(newText);
                return true;
            }
        });
    }

    private void submit(String query) {
        if(!"".equals(query) && !TextUtils.isEmpty(query)){
            if("ST0000001".equals(query)){
                Intent intent = new Intent(LogisticsActivity.this,OrderActivity.class);
                LogisticsActivity.this.startActivity(intent);
            }
        }else{

        }
    }
    private void change(String newText) {


    }
    private void exit_() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogisticsActivity.this.finish();
            }
        });
    }

    private void getWhole() {
        String url = "prod-api/api/logistics-inquiry/logistics_company/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, FenLeiData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                FenLeiData fenLeiData = (FenLeiData)obj;
                if(fenLeiData.getCode() == 200){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            fenlei.setLayoutManager(new GridLayoutManager(LogisticsActivity.this,4));
                            fenlei.setAdapter(new FenLeiAdapter(fenLeiData.getData(),LogisticsActivity.this));
                            arrange.setLayoutManager(new LinearLayoutManager(LogisticsActivity.this));
                            arrange.setAdapter(new ArrangeAdapter(fenLeiData.getData(),LogisticsActivity.this));
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void getBanner() {
        String url = "prod-api/api/logistics-inquiry/ad-banner/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, BannerData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                BannerData bannerData = (BannerData)obj;
                if(bannerData.getCode() == 200){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ArrayList<String> list = new ArrayList();
                            for (BannerData.DataDTO datum : bannerData.getData()) {
                                list.add(datum.getImgUrl());
                            }
                            BannerTool banner = new BannerTool(LogisticsActivity.this,list,banner1);
                            banner1.setAdapter(banner);
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void init() {
        exit = findViewById(R.id.exit);
        banner1 = findViewById(R.id.banner);
        fenlei = findViewById(R.id.fen_lei);
        search = findViewById(R.id.search);
        arrange = findViewById(R.id.fen_lei02);
    }
}